<!-- 前置机列表 -->
<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            label-width="90px"
        >
            <el-form-item label="前置机名称" prop="sysName">
                <el-input
                    v-model="queryParams.sysName"
                    placeholder="前置机名称"
                    clearable
                    size="small"
                />
            </el-form-item>
            <el-form-item label="注册码" prop="registerCode">
                <el-input
                    v-model="queryParams.registerCode"
                    placeholder="注册码"
                    clearable
                    size="small"
                />
            </el-form-item>
            <el-form-item label="前置机状态" prop="configStatus">
                <el-select
                    v-model="queryParams.configStatus"
                    clearable
                    size="small"
                    style="width: 140px"
                >
                    <el-option
                        v-for="dict in startOptions"
                        :key="dict.dictCode"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >搜索</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    >新增</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    >删除</el-button
                >
            </el-col>
        </el-row>

        <el-table
            v-loading="loading"
            :data="CarInoutList"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="前置机名称" align="center" prop="sysName" />
            <el-table-column label="单位" align="center" prop="companyName" />
            <el-table-column label="前置机编码" align="center" prop="registerCode" />
            <el-table-column label="前置机状态" width="100" align="center" prop="configStatus" />
            <el-table-column label="管理人员" width="100" align="center" prop="manager"/>
            <el-table-column label="手机号" width="120" align="center" prop="phoneNo" />
            <el-table-column label="设备数量"  width="100" align="center" prop="deviceCount" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column
                label="操作" width="200"
                align="center"
                class-name="small-padding fixed-width"
            >
                <template slot-scope="scope">
                     <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="showDeviceDialog(scope.row)"
                        >设备管理</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        >修改</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
        >
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="100px"
            >
                <el-form-item label="前置机名称" prop="sysName">
                    <el-input
                        v-model="form.sysName"
                        placeholder="请输前置机名称"
                        maxlength="20"
                    />
                </el-form-item>
                <el-form-item label="单位选择" prop="companyId" maxlength="30">
                    <treeselect
                        v-model="form.companyId"
                        :options="deptOptions"
                        :disable-branch-nodes="false"
                        :show-count="true"
                        placeholder="请选择归属部门"
                    />
                </el-form-item>
                <el-form-item label="前置机编码" prop="registerCode">
                    <el-input
                        v-model="form.registerCode"
                        placeholder="请输入前置机编码"
                        maxlength="64"
                    />
                </el-form-item>
                <el-form-item label="管理人员" prop="manager">
                    <el-input
                        v-model="form.manager"
                        placeholder="请输入管理人员"
                        maxlength="20"
                    />
                </el-form-item>
                <el-form-item label="手机号" prop="phoneNo">
                    <el-input
                        v-model="form.phoneNo"
                        placeholder="请输入手机号"
                        maxlength="11"
                    />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!--  设备管理列表 -->
        <el-dialog
            title="设备管理"
            :visible.sync="deviceTable"
            width="800px"
            append-to-body
        >
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="addDevice"
                        >新增</el-button
                    >
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple2"
                        @click="deciceDelete"
                        >删除</el-button
                    >
                </el-col>
            </el-row>
            <el-table
                v-loading="loading"
                :data="deviceCollectList"
                @selection-change="handleSelectionChange2"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    label="设备名称"
                    align="center"
                    prop="deviceName"
                />
                <el-table-column label="设备ID" align="center" prop="id" />
                <el-table-column
                    label="设备编号"
                    align="center"
                    prop="deviceNo"
                />
                <el-table-column
                    label="设备说明"
                    align="center"
                    prop="remark"
                />
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="deciceUpdate(scope.row)"
                            >修改</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                v-show="detailtotal > 0"
                :total="detailtotal"
                :page.sync="detailParams.pageNum"
                :limit.sync="detailParams.pageSize"
                @pagination="getDeviceCollect"
            />
        </el-dialog>

        <!-- 设备添加对话框 -->
        <el-dialog
            :title="dcicetitle"
            :visible.sync="addDecice"
            width="500px"
            append-to-body
        >
            <el-form
                ref="devform"
                :model="devform"
                :rules="rules2"
                label-width="100px"
            >
                <el-form-item label="设备名称" prop="deviceName">
                    <el-input
                        v-model="devform.deviceName"
                        placeholder="请输入设备名称"
                        maxlength="20"
                    />
                </el-form-item>
                <el-form-item label="设备编号" prop="deviceNo">
                    <el-input
                        v-model="devform.deviceNo"
                        placeholder="请输入设备编号"
                        maxlength="64"
                    />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="devform.remark" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addDeciceForm"
                    >确 定</el-button
                >
                <el-button @click="deciceCancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getDevicelist, addDevice, editDevice, delDevice, getDeviceCollect, addDeviceCollect, editDeviceCollect, delDeviceCollect } from "@/api/device"
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
    name: "device",
    components: { Treeselect },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 选中数组
            ids2: [],
            // 非单个禁用
            single2: true,
            // 非多个禁用
            multiple2: true,
            // 总条数
            total: 0,
            // 部门树选项
            deptOptions: [],
            startOptions: [],
            // 前置机列表
            CarInoutList: [],
            // 设备列表
            deviceCollectList: [],
            // 设备列表总条数
            detailtotal: 0,
            // 弹出层标题
            title: "",
            dcicetitle: "",
            // 是否显示弹出层
            open: false,
            deviceTable: false,
            addDecice: false,
            //基站列表
            StationList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                sysName: '',
                registerCode: ''
            },
            // 查询参数
            detailParams: {
                pageNum: 1,
                pageSize: 10,
                configId: ''
            },
            configIdTemp: '',  // 临时前置机ID
            // 表单参数
            form: {},
            devform: {},
            // 表单校验
            rules: {
                sysName: [
                    { required: true, message: "前置机名称不能为空", trigger: ['blur', 'change'] }
                ],
                companyId: [
                    { required: true, message: "单位不能为空", trigger: ['blur', 'change'] }
                ],
                registerCode: [
                    { required: true, message: "前置机编码不能为空", trigger: ['blur', 'change'] }
                ]
            },
            rules2: {
                deviceName: [
                    { required: true, message: "设备名称不能为空", trigger: ['blur', 'change'] }
                ],
                deviceNo: [
                    { required: true, message: "设备编号不能为空", trigger: ['blur', 'change'] }
                ]
            }
        };
    },
    created () {
        this.getList();
        this.getTreeselect();
        this.getDicts("device_config_status").then(response => {
            this.startOptions = response.data;
        });
    },
    methods: {
        /** 查询部门下拉树结构 */
        getTreeselect () {
            treeselect().then(response => {
                this.deptOptions = response.data;
            });
        },
        /** 查询记录 */
        getList () {
            this.loading = true;
            getDevicelist(this.queryParams).then(response => {
                this.CarInoutList = response.data;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 查询设备列表 */
        getDeviceCollect () {
            getDeviceCollect(this.detailParams).then(response => {
                this.deviceCollectList = response.data;
                this.detailtotal = response.total;
            });
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                sysName: undefined,
                registerCode: undefined,
                phoneNo: undefined,
                manager: undefined,
                companyId: undefined,
                remark: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.reset();
            this.open = true;
            this.title = "添加前置机";
        },
        /** 添加车辆进出信息 */
        submitForm: function () {
            var reg = /^[0-9a-zA-Z]+$/
            let str = this.form.registerCode;
            if(!reg.test(str)){
                this.$message.error("前置机编码只能是字母或数字");
                return;
            }

            this.$refs["form"].validate(valid => {
                if (valid) {
                    // console.log(this.form)
                    if (this.form.id != undefined) {
                        editDevice(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            }
                        });
                    } else {
                        addDevice(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            }
                        });
                    }
                }
            });
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            this.reset();
            this.form.id = row.id;
            this.form.sysName = row.sysName;
            this.form.registerCode = row.registerCode;
            this.form.phoneNo = row.phoneNo;
            this.form.manager = row.manager;
            this.form.companyId = row.companyId;
            this.form.remark = row.remark;
            // this.form = Object.assign(this.form, row);
            this.title = "修改前置机";
            this.open = true;
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const postIds = row.id || this.ids;
            this.$confirm('是否确认删除此数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                // console.log(postIds);
                return delDevice(postIds);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function () { });
        },
        // 详情列表多选框选中数据
        handleSelectionChange2 (selection) {
            this.ids2 = selection.map(item => item.id);
            this.single2 = selection.length != 1;
            this.multiple2 = !selection.length;
        },
        showDeviceDialog (row) { //显示设备列表
            this.deviceTable = true;
            this.detailParams.configId = row.id;
            this.configIdTemp = row.id;
            this.getDeviceCollect();
        },
        //显示添加设备弹框
        addDevice () {
            this.devform = {
                configId: this.configIdTemp,
                deviceName: undefined,
                deviceNo: undefined,
                remark: undefined
            };
            this.dcicetitle = "添加设备";
            this.resetForm("devform");
            this.addDecice = true;
        },
        deciceUpdate (row) {

            this.devform.id = row.id;
            this.devform.configId = row.configId;
            this.devform.deviceName = row.deviceName;
            this.devform.deviceNo = row.deviceNo;
            this.devform.remark = row.remark;

            this.dcicetitle = "修改设备";
            this.addDecice = true;
        },
        addDeciceForm(){

            var reg = /^[0-9a-zA-Z]+$/
            let str = this.devform.deviceNo;
            if(!reg.test(str)){
                this.$message.error("设备编码只能是字母或数字");
                return;
            }

            this.$refs["devform"].validate(valid => {
                if (valid) {
                    // console.log(this.devform)
                    if (this.devform.id != undefined) {
                        editDeviceCollect(this.devform).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.addDecice = false;
                                this.getDeviceCollect();
                            }
                        });
                    } else {
                        addDeviceCollect(this.devform).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.addDecice = false;
                                this.getDeviceCollect();
                            }
                        });
                    }
                }
            });
        },
        deciceCancel () {
            this.addDecice = false;
        },
        /** 设备删除操作 */
        deciceDelete (row) {
            const postIds = row.id || this.ids2;
            this.$confirm('是否确认删除此数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delDeviceCollect(postIds);
            }).then(() => {
                this.getDeviceCollect();
                this.msgSuccess("删除成功");
            }).catch(function () { });
        },
    }
};
</script>
<style scoped>
.vue-treeselect__control {
    height: 32px !important;
}
.vue-treeselect__input {
    height: 32px !important;
}
</style>